package net.sansi.v3correctserver.service.v3serverapi;

import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.interceptor.Interceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Base64Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by Fang
 * create time : 2024/11/5
 * IDE : IntelliJ IDEA
 * <p>
 * V3ServerApi拦截器
 */
@Slf4j
@Component
public class V3ServerApiInterceptor<T> implements Interceptor<T> {

    @Value("${correct.v3-server.account}")
    private String account;

    @Value("${correct.v3-server.password}")
    private String password;

    /**
     * 获取认证信息
     *
     * @return 认证
     */
    public final String getAuthorization() {
        return "Basic " + Base64Util.encode(account + ":" + password);
    }

    /**
     * 请求前拦截
     *
     * @param req 请求对象
     * @return 是否继续执行
     */
    @Override
    public boolean beforeExecute(ForestRequest req) {
        req.addHeader("Authorization", getAuthorization());
        return true;
    }
}
